package com.freshmarket.freshmarket.service.impl;

import com.freshmarket.freshmarket.entity.Manager;
import com.freshmarket.freshmarket.dao.ManagerMapper;
import com.freshmarket.freshmarket.exception.MyException;
import com.freshmarket.freshmarket.service.IManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freshmarket.freshmarket.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@Service
public class ManagerServiceImpl implements IManagerService {


    @Resource
    private ManagerMapper managerMapper;

    //管理员最后登录时间
    @Override
    public Result updateManagerLoginTime(String managerName, String managerLastLogintime) throws Exception{
        int row = managerMapper.updateManagerLoginTime(managerName, managerLastLogintime);
         if(row>0){
            return new Result(true,200,"登录成功！",null);
        }else{
            throw new MyException("登录时间跟新失败");
        }

    }

    /**
     * 管理员注册
     * @param manager
     * @return result
     */
    @Override
    public Result insertManager(Manager manager) throws Exception{

        Manager mang = managerMapper.selectByName(manager.getManagerName());
        if(mang == null){
            //新增
            int row = managerMapper.insertManager(manager.getManagerName(), manager.getManagerPassword());
            if(row>0){
                return new Result(true,200,"注册成功！",null);
            }else{
                return new Result(false,444,"注册失败！",null);
            }
        }else{
            return new Result(false,444,"该用户已注册！",null);
        }


    }
}
